package com.swotplus.admin.web.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swotplus.framework.web.BaseController;
import com.swotplus.common.annotation.Log;
import com.swotplus.common.core.AjaxResult;
import com.swotplus.common.enums.BusinessType;
import com.swotplus.common.enums.OnlineStatus;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.framework.shiro.ShiroUtils;
import com.swotplus.framework.shiro.session.OnlineSession;
import com.swotplus.framework.shiro.session.OnlineSessionDAO;
import com.swotplus.system.sys.entity.SysUserOnline;
import com.swotplus.system.sys.service.SysUserOnlineService;
import com.swotplus.system.sys.vo.SysUserOnlineVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 在线用户监控
 * 
 * @author hugang
 */
@Controller
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController
{
    private String prefix = "monitor/online";

    @Autowired
    private SysUserOnlineService sysUserOnlineService;

    @Autowired
    private OnlineSessionDAO onlineSessionDAO;

    @RequiresPermissions("monitor:online:view")
    @GetMapping()
    public String online()
    {
        return prefix + "/online";
    }

    @RequiresPermissions("monitor:online:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(SysUserOnlineVo vo)
    {
        Page<SysUserOnline> page=new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<SysUserOnline> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(vo.getLoginName())){
            queryWrapper.eq("login_name",vo.getLoginName());
        }
        IPage<SysUserOnline> pageList= sysUserOnlineService.page(page,queryWrapper);
        return AjaxResult.success("成功", (int) pageList.getTotal(),pageList.getRecords());
    }

    @RequiresPermissions("monitor:online:batchForceLogout")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @PostMapping("/batchForceLogout")
    @ResponseBody
    public AjaxResult batchForceLogout(@RequestParam("ids[]") String[] ids)
    {
        for (String sessionId : ids)
        {
            SysUserOnline online = sysUserOnlineService.getById(sessionId);
            if (online == null)
            {
                return error("用户已下线");
            }
            OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getId());
            if (onlineSession == null)
            {
                return error("用户已下线");
            }
            if (sessionId.equals(ShiroUtils.getSessionId()))
            {
                return error("当前登陆用户无法强退");
            }
            onlineSession.setStatus(OnlineStatus.off_line);
            onlineSessionDAO.update(onlineSession);
            online.setStatus(OnlineStatus.off_line);
            sysUserOnlineService.save(online);
        }
        return success();
    }

    @RequiresPermissions("monitor:online:forceLogout")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @PostMapping("/forceLogout")
    @ResponseBody
    public AjaxResult forceLogout(String sessionId)
    {
        SysUserOnline online = sysUserOnlineService.getById(sessionId);
        if (sessionId.equals(ShiroUtils.getSessionId()))
        {
            return error("当前登陆用户无法强退");
        }
        if (online == null)
        {
            return error("用户已下线");
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getId());
        if (onlineSession == null)
        {
            return error("用户已下线");
        }
        onlineSession.setStatus(OnlineStatus.off_line);
        onlineSessionDAO.update(onlineSession);
        online.setStatus(OnlineStatus.off_line);
        sysUserOnlineService.save(online);
        return success();
    }
}
